package IOTWeb.com.agreementclient;

import org.apache.commons.lang.StringUtils;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import IOTWeb.com.IOTMain;
import IOTWeb.com.utils.GetProperties;
import IOTWeb.com.utils.H2ConnectionPool;
import IOTWeb.com.utils.JDUtil;
import IOTWeb.com.utils.MyClassLoader;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
 * MQTT������
 * @author xu-zhiqiang
 * @create 2021-10-10 9:21
 */

public class SqlPushCallback implements MqttCallback {
    // ���Ӷ���
    private Connection conn;
    
	public void connectionLost(Throwable cause) { 
		//����h2���ݿ�
        // ���Ӷ�ʧ��һ�����������������  
        System.out.println("���ӶϿ�������������");  
    }  
    
    public void deliveryComplete(IMqttDeliveryToken token) {
        System.out.println("deliveryComplete---------" + token.isComplete());  
    }
 
    public void messageArrived(String topic, MqttMessage message) throws Exception {
    	JDUtil jDUtil = new JDUtil();
    	H2ConnectionPool h2 = new H2ConnectionPool();
        // subscribe��õ�����Ϣ��ִ�е�������  
        System.out.println("������Ϣ���� : " + topic);  
        System.out.println("������ϢQos : " + message.getQos());  
        System.out.println("������Ϣ���� : " + new String(message.getPayload())); 
       
        //��ȡ��ǰʱ��
        String t = new SimpleDateFormat("yyyy/MM/dd-HH").format(new Date());
        System.out.println(t);
       
        //����
        String value = new String(message.getPayload());
        String[] split = StringUtils.splitPreserveAllTokens(value,"-");
        for(String s: split) {
        	System.out.println(s);
        }    
        String name = split[0];//�õ���̬��������
        
        //���ض�̬��
        String add = null;
		if(IOTMain.ADDRESS == "linux")
			add = h2.selectOneConfig("Linux.Fileadd");
		else
			add = h2.selectOneConfig("windows.Fileadd");
		MyClassLoader urlClassLoader = new MyClassLoader(add);
		Class<?> classs =  urlClassLoader.loadClass(name);
		Object newInstance= classs.newInstance();
			
		//������
		Method m = classs.getMethod("analysis", String.class);
		Map<String, String> map =(Map<String, String>) m.invoke(newInstance, split[1]);
		System.out.println("�����������ݣ�"+map);
		Map<Integer,String> mper = new HashMap<>();
			
		//�洢��
		String a = "analysis";
		Method s = classs.getMethod("sqls", String.class, Map.class);
		System.out.println("sqls��"+s.invoke(newInstance, a,map));
		mper = (Map<Integer,String>)s.invoke(newInstance, a,map);
			
		//���ݿ���д��
		if(!mper.isEmpty()) {
		Method wr = classs.getMethod("adds", Map.class);
		wr.invoke(newInstance, mper);
		}
		//������ܱ�
		Method colnum = classs.getMethod("colnum", Map.class);
		colnum.invoke(newInstance, map);
		System.out.println("�ɹ��������ݿ�");
		
			
		//����MySQL��contable
		Set<String> keySet = map.keySet();
		Iterator<String> it =keySet.iterator();
		while(it.hasNext()){
			 String key = it.next();
			 String valuek = map.get(key);
			 if(key.equals("ID")) {
				 int va = Integer.parseInt(valuek);
			     conn = jDUtil.getConnection("URLDev");
			     if (conn == null)
			     	return;
			            // ����һ��Statement����
			     String AddSQL = "INSERT INTO contable VALUES (?,?);";
			     PreparedStatement ps = conn.prepareStatement(AddSQL);
			     ps.setInt(1, va);
			     ps.setString(2, "new"+key);
			            // ִ��sql���
			     ps.executeUpdate();
			     jDUtil.releaseConnection(conn, ps, null);
			}
		}

    }
}
